import domain.Brand;
import mapper.BrandMapper;
import mapper.BrandMapper2;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.BeforeClass;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

public class BrandText {
    private static SqlSessionFactory sqlSessionFactory;

    @BeforeClass
    public static void setUpBeforeClass() throws Exception {
        // 1. 定义配置文件的位置
        String resource = "mybatis-config.xml";
        // 2. 获取这个文件的输入流
        InputStream inputStream = Resources.getResourceAsStream(resource);
        // 3. 基于配置文件构建SqlSessionFactory
        sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream, "dev");
    }

    @Test
    public void selectAllTest() throws IOException {
        // 1. 定义配置文件的位置
        String resource = "mybatis-config.xml";
        // 2. 获取这个文件的输入流
        InputStream inputStream = Resources.getResourceAsStream(resource);
        // 3. 基于配置文件构建SqlSessionFactory
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream, "dev");
        try (
                SqlSession sqlSession = sqlSessionFactory.openSession()
        ) {
            BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
            List<Brand> list = brandMapper.selectAll();
            list.forEach(System.out::println);
        }
    }

    @Test
    public void selectOneTest() throws IOException {
        try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
            BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
//            Brand brand = brandMapper.selectById(1,"order by id asc");
            Brand brand = brandMapper.selectById(1);
            System.out.println(brand);
        }
    }


    @Test
    public void selectByConditionTest() throws IOException {
        try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
            BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
//            Brand brand = brandMapper.selectById(1,"order by id asc");
//            List<Brand> list = brandMapper.selectByCondition("小米","小米科技有限公司",1);
            Brand brand = new Brand();
            brand.setBrandName("小米");
            brand.setStatus(1);
            brand.setCompanyName("小米科技有限公司");
            List<Brand> list = brandMapper.selectByCondition(brand);
            System.out.println(list);
        }
    }


    //    批量插入
    @Test
    public void bathInsertTest() throws IOException {
        List<Brand> list = new ArrayList<>();
        list.add(new Brand(
                "百度",
                "百度什么什么有限公司",
                50,
                "百事通",
                1
        ));
        list.add(new Brand(
                "百度",
                "百度什么什么有限公司",
                50,
                "百事通",
                1
        ));
        list.add(new Brand(
                "百度",
                "百度什么什么有限公司",
                50,
                "百事通",
                1
        ));
        list.add(new Brand(
                "百度",
                "百度什么什么有限公司",
                50,
                "百事通",
                1
        ));
        try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
            BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
            brandMapper.batchInsert(list);
            sqlSession.commit();
        }
    }

    @Test
    public void selAllAoo() {
        try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
            BrandMapper2 brandMapper = sqlSession.getMapper(BrandMapper2.class);
            System.out.println(brandMapper.selectAllBrand());
        }
    }

    @Test
    public void insertAoo() throws IOException {
        try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
            BrandMapper2 brandMapper = sqlSession.getMapper(BrandMapper2.class);
            Brand brand = new Brand("百度",
                    "么有限公司",
                    50,
                    "百事通",
                    1);
            brandMapper.insertBrand(brand);
            sqlSession.commit();
        }
    }
    
}
